package com.axzhengxin.platform.homePage.service.impl;

import com.axzhengxin.platform.business.mapper.BizApproveInfoMapper;
import com.axzhengxin.platform.business.mapper.BizFlowInfoMapper;
import com.axzhengxin.platform.homePage.domain.MonthResult;
import com.axzhengxin.platform.homePage.domain.SearchParams;
import com.axzhengxin.platform.homePage.domain.StatisticData;
import com.axzhengxin.platform.homePage.mapper.HomePageMapper;
import com.axzhengxin.platform.homePage.service.IHomePageService;
import com.ruoyi.common.annotation.DataScope;
import com.ruoyi.common.utils.SecurityUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.text.NumberFormat;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.List;

/**
 * @Author AHZJ0005
 * @Date 2021/3/2-19:34
 **/
@Slf4j
@Service
public class HomeServiceImpl implements IHomePageService {

    @Autowired
    private BizApproveInfoMapper bizApproveInfoMapper;
    @Autowired
    private BizFlowInfoMapper bizFlowInfoMapper;

    @Autowired
    private HomePageMapper homePageMapper;

    @Override
    @DataScope(deptAlias = "sd", userAlias = "uu")
    public int getTaskAgentCount(SearchParams searchParams) {
        return bizFlowInfoMapper.getTaskAgentCount(searchParams);
    }

    @Override
    @DataScope(deptAlias = "dt", userAlias = "dt")
    public List<StatisticData> getAuditingStatistic(SearchParams searchParams) {

        return getStatistic(searchParams);
    }

    @Override
    @DataScope(deptAlias = "dt", userAlias = "dt")
    public List<StatisticData> getApproveStatistic(SearchParams searchParams) {

        return getStatistic(searchParams);
    }

    @Override
    @DataScope(deptAlias = "t", userAlias = "t")
    public List<MonthResult> monthResults(MonthResult result) {
        //------------------待审核----------------------
        //查询当月数据
        //查询上月数据
        if (SecurityUtils.isAdmin(SecurityUtils.getLoginUser().getUser().getUserId())) {
            result.setIsAdmin(0);
        }else {
            result.setIsAdmin(1);
        }
        MonthResult auditingCountByMonth = homePageMapper.getNewAuditingCountByMonth(result);
        MonthResult auditingCountByLastMonth = homePageMapper.getNewAuditingCountByLastMonth(result);


        //查询当日数据
        //查询昨日数据
        MonthResult auditingCountByNowDay = homePageMapper.getNewAuditingCountByNowDay(result);
        MonthResult auditingCountByYesterday = homePageMapper.getNewAuditingCountByYesterday(result);


        MonthResult monthResult1 =
                dealMonthResultCount(auditingCountByMonth, auditingCountByLastMonth, auditingCountByNowDay, auditingCountByYesterday);
        monthResult1.setUpTheme("月申请新增数");
        monthResult1.setDownTheme("日申请新增");
        MonthResult monthResult2 =
                delMothResultMount(auditingCountByMonth, auditingCountByLastMonth, auditingCountByNowDay, auditingCountByYesterday);
        monthResult2.setUpTheme("月申请新增金额");
        monthResult2.setDownTheme("日申请金额新增");
        //------------------待授信---(改为已授信/20210415/xps)----------------------
        //查询当月数据
        //查询上月数据
        MonthResult approveCountByMonth = homePageMapper.getNewApproveCountByMonth(result);
        MonthResult approveCountByLastMonth = homePageMapper.getNewApproveCountByLastMonth(result);
        //查询当日数据
        //查询昨日数据
        MonthResult approveCountByNowDay = homePageMapper.getNewApproveCountByNowDay(result);
        MonthResult approveCountByYesterday = homePageMapper.getNewApproveCountByYesterday(result);


        MonthResult monthResult3 =
                dealMonthResultCount(approveCountByMonth, approveCountByLastMonth, approveCountByNowDay, approveCountByYesterday);
        monthResult3.setUpTheme("月授信新增数");
        monthResult3.setDownTheme("日授信新增");
        MonthResult monthResult4 =
                delMothResultMount(approveCountByMonth, approveCountByLastMonth, approveCountByNowDay, approveCountByYesterday);
        monthResult4.setUpTheme("月授信新增金额");
        monthResult4.setDownTheme("日授信金额新增");
        List<MonthResult> monthResults = new ArrayList<>();
        monthResults.add(monthResult1);
        monthResults.add(monthResult2);
        monthResults.add(monthResult3);
        monthResults.add(monthResult4);
        return monthResults;
    }

    @Override
    @DataScope(deptAlias = "d", userAlias = "u")
    public int getWarnCount(SearchParams searchParams) {
        return homePageMapper.getWarnCount(searchParams);
    }


    /**
     * 处理次数方法
     *
     * @return 对象
     */
    public MonthResult dealMonthResultCount(MonthResult approveCountByMonth, MonthResult approveCountByLastMonth, MonthResult approveCountByNowDay, MonthResult approveCountByYesterday) {
        long upMonthCount = approveCountByMonth.getUpMonthCount();

        long lastMonthCount = approveCountByLastMonth.getUpMonthCount();

        String monthSubCount = getSubCount(upMonthCount, lastMonthCount);

        long upDayCount = approveCountByNowDay.getUpDayCount();

        long lastDayCount = approveCountByYesterday.getUpDayCount();

        String daySubCount = getSubCount(upDayCount, lastDayCount);
        return setMonthResult(String.valueOf(upDayCount), String.valueOf(upMonthCount), daySubCount, monthSubCount);
    }


    /**
     * 处理金额方法
     *
     * @return 对象
     */
    public MonthResult delMothResultMount(MonthResult approveCountByMonth, MonthResult approveCountByLastMonth, MonthResult approveCountByNowDay, MonthResult approveCountByYesterday) {
        Long upMonthAmount = approveCountByMonth.getUpMonthAmount();

        Long lastMonthAmount = approveCountByLastMonth.getUpMonthAmount();
        //月环比
        String monthSubAmount = getSubCount(upMonthAmount, lastMonthAmount);

        Long upDayAmount = approveCountByNowDay.getUpDayAmount();

        Long lastDayAmount = approveCountByYesterday.getUpDayAmount();
        //日环比

        String daySubAmount = getSubCount(upDayAmount, lastDayAmount);
        //数量


        return setMonthResult(String.valueOf(upDayAmount), String.valueOf(upMonthAmount), daySubAmount, monthSubAmount);
    }

    public String getSubCount(long nowDayCount, long yesterdayCount) {
        NumberFormat instance = NumberFormat.getInstance();
        instance.setMaximumFractionDigits(2);
        if (yesterdayCount == 0 && nowDayCount != 0) {
            return "100%";
        }
        if (yesterdayCount == 0 && nowDayCount == 0) {
            return "0%";
        }
        String format = instance.format(((float) nowDayCount - (float) yesterdayCount) / (float) yesterdayCount * 100);
        if ("0".equals(format) && nowDayCount != 0) {
            //数据过小取近似
            format = "0.01";
        }
        return format + "%";
    }


    public MonthResult setMonthResult(String upDayValue, String upMonthValue, String upDaySub, String upMothSub) {
        MonthResult monthResult = new MonthResult();
        monthResult.setUpDayValue(upDayValue);
        monthResult.setUpMonthValue(upMonthValue);
        monthResult.setUpDaySub(upDaySub);
        monthResult.setUpMonthSub(upMothSub);
        return monthResult;
    }

    public List<StatisticData> getStatistic(SearchParams searchParams) {
        Calendar calendar = Calendar.getInstance();
        List<StatisticData> list = new ArrayList<>();

        for (int i = 0; i <= 5; i++) {
            calendar.setTime(new Date());
            calendar.add(Calendar.DAY_OF_MONTH, -i);
            Date time = calendar.getTime();
            StatisticData statisticData;
            Integer flag = searchParams.getFlag();
            searchParams.setDayTime(i);
            if (flag == 1) {
                if (SecurityUtils.isAdmin(SecurityUtils.getLoginUser().getUser().getUserId())) {
                    statisticData = homePageMapper.selectAuditingCountByDateAdmin(searchParams);
                } else {
                    statisticData = homePageMapper.selectAuditingCountByDate(searchParams);
                }
            } else {
                statisticData = homePageMapper.selectApproveCountByDate(searchParams);
            }
            if (statisticData == null) {
                statisticData = new StatisticData();
                statisticData.setCount(0);
                statisticData.setAmount("0");
            } else if (statisticData.getAmount() != null) {
                String[] split = statisticData.getAmount().split("\\.");
                statisticData.setAmount(split[0]);
            } else {
                statisticData.setAmount("0");
            }
            statisticData.setDate(new SimpleDateFormat("MM-dd").format(time));
            list.add(statisticData);
        }
        return list;
    }

}
